import {Component, Injector, OnInit, ViewChild} from '@angular/core';
import { IFlowDetailParam } from '@app/shared/common/workflow/workflow-common-interface';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { DashboardServiceProxy, GetAllDashboardDataDto, PagedPaperDto, PagedPaperInput, WorkFlowQueryServiceProxy, WorkFlowQueryType } from '@shared/service-proxies/service-proxies';
import { WorkflowDetailComponent } from '../workflow/workflow-detail.component';
import { DashboardMorelistModalComponent } from './dashboard.morelist.component';

@Component({
    templateUrl: './dashboard.component.html',
    styleUrls:['./dashboard.component.css'],
    animations: [appModuleAnimation()]
})
export class DashboardComponent extends AppComponentBase implements OnInit {

    @ViewChild('workFlowDetail', { static: true }) workflowDetailModal: WorkflowDetailComponent;
    @ViewChild('dashboardMorelist', { static: true }) morelistModal: DashboardMorelistModalComponent;
    wrokFlowPermission = true;

    previewNumData = {
        todo:0,
        releted:0
    }

    wfQueryType= WorkFlowQueryType
    workFlowDatas:{[key:string]:PagedPaperDto[]} = {
        todoList:[],
        relatedList:[]
    }

    allDashboardDataDto: GetAllDashboardDataDto = new GetAllDashboardDataDto()
    constructor(
        private  _WorkFlowQueryServiceProxy:WorkFlowQueryServiceProxy,
        private _dashboardServiceProxy: DashboardServiceProxy,
        injector: Injector,
    ){
        super(injector);
    }
    ngOnInit(): void {
        if (this.isGranted('Pages.Manage.WorkFlow')){
            this.getInitData();

        }
        this.getDisplayData();
    }


    goToDetailModel(item: PagedPaperDto){
        this.workflowDetailModal.show({ paperId: item.paperId});
    }

    backToList(item){
        this.getToDoList();
        this.getRelatedList();
    }


    goMoreList(type:WorkFlowQueryType,title:string) {
        
        this.morelistModal.show(type,title);
    }


    private getInitData(){
        this.getToDoList();
        this.getRelatedList();
    }

    
    private getDisplayData(){
        this._dashboardServiceProxy.getAllDashboardDataDto().subscribe(res=>{
            this.allDashboardDataDto = res;
        })
    }



    private getToDoList(){
        let todoListInput = new PagedPaperInput();
        todoListInput.wfQueryType = WorkFlowQueryType.Todo;
        todoListInput.skipCount = 0;
        todoListInput.maxResultCount = 10; 
        this._WorkFlowQueryServiceProxy.getPagedPapers(todoListInput).subscribe(result=>{
            this.workFlowDatas.todoList = result.items;
            this.previewNumData.todo = result.totalCount;
        });
    }


    private getRelatedList(){
        let relatedListInput = new PagedPaperInput();
        relatedListInput.wfQueryType = WorkFlowQueryType.Related;
        relatedListInput.skipCount = 0;
        relatedListInput.maxResultCount = 10; 

        this._WorkFlowQueryServiceProxy.getPagedPapers(relatedListInput).subscribe(result=>{
            this.workFlowDatas.relatedList = result.items;
            this.previewNumData.releted = result.totalCount;
        });
    }



}
